layui.define(['table', 'form', 'laypage', 'laytpl', 'layer', 'laydate'], function (exp) {
    var table = layui.table
        , form = layui.form
        , $ = layui.$
        , laydate = layui.laydate
        , layer = layui.layer
        , laytpl = layui.laytpl;

    const token = layui.data('xunyilu').token

    // 渲染数据表格
    let usersTable = table.render({
        id: 'usersTable',
        elem: '#users-table',
        url: '/admin/page',
        method: 'get',
        headers: {'token': layui.data('xunyilu').token},
        toolbar: "#users-toolbar",
        defaultToolbar: ['filter', 'print'],
        request: {
            pageName: 'page' // 页码的参数名称，默认：page
            , limitName: 'size' // 每页数据量的参数名，默认：limit
        },
        parseData: function (res) {
            return {
                "data": res.payload.records,
                'code': res.status,
                'msg': res.msg,
                "count": res.payload.total
            }
        },
        response: {
            statusCode: 200
        },
        loading: true,
        cols: [[
            {type: 'checkbox', fixed: 'left'},
            {type: 'numbers', field: 'id', sort: true, title: '序号', align: 'center', width: '5%'},
            {field: 'name', align: 'center', title: '用户名'},
            {field: 'email', align: 'center', title: '邮箱'},
            {field: 'time', align: 'center', title: '上次登录时间'},
            {align: 'center', title: '性别', field: 'sex', templet: function (d) {
                    if (d.sex === 'MAN') {
                        return '<span class="layui-badge layui-bg-blue">男</span>';
                    } else {
                        return '<span class="layui-badge layui-bg-green">女</span>'
                    }
                }
            },
            {align: 'center', title: '审核状态', field: 'register', templet: function (d) {
                if (d.register === 'IS_REGISTER') {
                    return '<span class="layui-badge layui-bg-green">已通过</span>';
                } else {
                    return '<span class="layui-badge layui-bg-gray">未审核</span>'
                }
            }},
            {title: '操作', align: 'center', fixed: 'right', toolbar: '#users-bar', width: 320}
        ]],
        page: true,
        limits: [2, 4, 6, 8, 10, 20]
        , text: {
            none: '还没有用户!'
        }, done: function (res, curr, count) {
            this.where = {};
        }
    });

    // 监听列工具条
    table.on('tool(users-table)', function (obj) {
        const data = obj.data,
            event = obj.event,
            tr = obj.tr;
        if (event === 'del') {
            let loadIndex = layer.load();
            $.post('/admin/delete/' + data.id, {"token": token}, function (result) {
                layer.close(loadIndex);
                if (result.success) {
                    layer.msg('删除成功!');
                    obj.del()
                } else {
                    layer.msg(result.msg)
                }
            })
        } else if (event === 'register') {
            let loadIndex = layer.load();
            $.post('/admin/authorize/' + data.id, {"register": "IS_REGISTER", "token": token}, function (result) {
                layer.close(loadIndex);
                if (result.success) {
                    $(tr[2]).find('button[lay-event="register"]').addClass('layui-btn-disabled').prop('disabled', true)
                    $(tr[2]).find('button[lay-event="notRegister"]').removeClass('layui-btn-disabled').prop('disabled', false)
                    layer.msg(data.name + '审核通过！')
                    obj.update({
                        register: "IS_REGISTER"
                    })
                }

            })
        } else if (event === 'notRegister') {
            let loadIndex = layer.load();
            $.post('/admin/authorize/' + data.id, {"register": "NO_REGISTER", "token": token}, function (result) {
                layer.close(loadIndex);

                if (result.success) {
                    $(tr[2]).find('button[lay-event="notRegister"]').addClass('layui-btn-disabled').prop('disabled', true)
                    $(tr[2]).find('button[lay-event="register"]').removeClass('layui-btn-disabled').prop('disabled', false)
                    layer.msg(data.name + '已否决！')
                    obj.update({
                        register: "NO_REGISTER"
                    })
                }

            })
        }
    });

    // 监听左上工具条
    table.on('toolbar(users-table)', function(obj){
        var checkStatus = table.checkStatus(obj.config.id);
        switch(obj.event){
            case 'delete':
                if (checkStatus.data.length === 0) {
                    layer.msg('请选择!', {icon: 5});
                    return;
                }
                layer.confirm('确定批量删除他们吗?', {icon: 3, title:'确认'}, function(index) {
                    var data = checkStatus.data
                        , ids = []
                        , loadIndex;
                    $.each(data, function (index, value) {
                        ids.push(value.id)
                    });
                    $.ajax({
                        url: '/admin/delete/more',
                        type: 'post',
                        dataType: 'json',
                        data: {
                            ids: ids,
                            "token": token
                        },
                        beforeSend: function() {
                            loadIndex = layer.load(1, {
                                shade: [0.1,'#fff'] //0.1透明度的白色背景
                            });
                        },
                        complete: function() {
                            layer.close(loadIndex);
                        },
                        success: function (data) {
                            layer.msg('批量删除成功!', {time: 1500});
                            usersTable.reload({
                                where: {
                                    page: 1,
                                    size: 10
                                }
                            })
                        },
                        error: function () {
                            layer.msg('删除失败!', {time: 1500});
                            usersTable.reload({
                                where: {
                                    page: 1,
                                    size: 10
                                }
                            })
                        }
                    });
                    layer.close(index);
                });
                break;
            case 'pass':
                if (checkStatus.data.length === 0) {
                    layer.msg('请选择!', {icon: 5});
                    return;
                }
                layer.confirm('确定批量通过他们吗?', {icon: 3, title:'确认'}, function(index) {
                    var data = checkStatus.data
                        , ids = []
                        , loadIndex;
                    loadIndex = layer.load();
                    $.each(data, function (index, value) {
                        ids.push(value.id)
                    });
                    $.post('/admin/authorize/more', {ids: ids, "register": "IS_REGISTER", "token": token}, function (result) {
                        layer.close(loadIndex)
                        if (result.success) {
                            layer.msg('批量同意成功!', {time: 1500});
                        } else {
                            layer.msg(result.msg, {time: 1500})
                        }
                        usersTable.reload({
                            where: {
                                page: 1,
                                size: 10
                            }
                        })
                    })
                    layer.close(index);
                });
                break;
            case 'notPass':
                if (checkStatus.data.length === 0) {
                    layer.msg('请选择!', {icon: 5});
                    return;
                }
                layer.confirm('确定批量否决他们吗?', {icon: 3, title:'确认'}, function(index) {
                    var data = checkStatus.data
                        , ids = []
                        , loadIndex;
                    loadIndex = layer.load();
                    $.each(data, function (index, value) {
                        ids.push(value.id)
                    });
                    $.post('/admin/authorize/more', {ids: ids, "register": "NO_REGISTER", "token": token}, function (result) {
                        layer.close(loadIndex)
                        if (result.success) {
                            layer.msg('批量否决成功!', {time: 1500});
                        } else {
                            layer.msg(result.msg, {time: 1500})
                        }
                        usersTable.reload({
                            where: {
                                page: 1,
                                size: 10
                            }
                        })
                    })
                    layer.close(index);
                });
                break;
        };
    });

    // 查询工具
    form.on('submit(search-users)', function (data) {
        table.reload('usersTable', {
            where: {
                name: data.field['name'],
                sex: data.field['sex'],
                register: data.field['register']
            }
        });
        form.render();
    });

    // 重置查询
    form.on('submit(users-search)', function (data) {
        form.val("users-form", {
            "name": ""
            , "sex": ""
            , "register": ""
        });
        usersTable.reload({
            where: {
                name: null,
                sex: null,
                register: null
            }
        });
        form.render();
    })

    exp('users', {});
});